<?php
namespace app\plus\controller;

use app\common\model\ShopGoodsModel;
use think\Controller;

class GoodsController extends Controller
{

    /**
     * 获取ajax商品数据
     */
    public function ajaxGoods()
    {
        $page    = $this->request->param('page/d',1);
        $pageSize = 8;
        $siteId    = $this->request->param('site_id/d');
        $goodsName = $this->request->param('query_goods_name');
        $skuids = $this->request->param('skuids');
        $map['is_presell'] = 0; //排除预售商品
        $map['promotion_type'] = 0; //排除活动商品
        if ($siteId > 0) {
            $map['site_id'] = $siteId;
        }
        if (!empty($goodsName)) {
            $map['goods_name'] = ['like', '%' . $goodsName . '%'];
        }
        $goodsModel = new ShopGoodsModel();
        $list       = $goodsModel->alias('t1')
            ->field('*')
            ->join('shop_goods_sku t2', 't1.goods_id=t2.goods_id')
            ->where($map)
            ->order('goods_sort desc')
            ->page($page, $pageSize)
            ->select();
        $count      = $goodsModel->alias('t1')
            ->join('shop_goods_sku t2', 't1.goods_id=t2.goods_id')
            ->where($map)
            ->count();
        $total = ceil($count/$pageSize);
        if (!$list->isEmpty()) {
            $list = $list->toArray();
            foreach ($list as $k => $v) {
                $info = format_goods_content($v);
                $list[$k] = $info;
                $list[$k]['json'] = json_encode([
                    'site_id'=>$info['site_id'],
                    'site_name'=>$info['site_name'],
                    'goods_id'=>$info['goods_id'],
                    'sku_id'=>$info['sku_id'],
                    'goods_name'=>$info['goods_name'],
                    'sku_name'=>$info['sku_name'],
                    'fulltitle'=>$info['fulltitle'],
                    'sub_title'=>$info['sub_title'],
                    'goods_thumb'=>$info['goods_thumb'],
                    'minimum'=>$info['minimum'],
                    'market_price'=>$info['market_price'],
                    'goods_price'=>$info['goods_price'],
                    'simple_price'=>$info['simple_price'],
                    'step'=>$info['step'],
                    'goods_freight'=>$info['goods_freight'],
                    'goods_stock'=>$info['goods_stock'],
                ]);
            }
        } else {
            $list = [];
        }
        $data['total'] =$total;
        $data['list'] =$list;
        return json($data);
    }

    /**
     * 单选商品
     * @return mixed
     */
    public function selectOne(){
        $siteId    = $this->request->param('site_id/d');
        $this->assign('site_id',$siteId);
        return $this->fetch();
    }
    /**
     * 多选商品
     * @return mixed
     */
    public function selectMore(){
        $siteId    = $this->request->param('site_id/d');
        $this->assign('site_id',$siteId);
        return $this->fetch();
    }
}